package com.yuqing.myblogServe.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuqing.myblogServe.common.lang.PageBean;
import com.yuqing.myblogServe.common.lang.Result;
import com.yuqing.myblogServe.dao.entity.*;
import com.yuqing.myblogServe.service.*;
import com.yuqing.myblogServe.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuqing
 * @since 2023-12-01
 */

@RestController
@RequestMapping("/article")
@Api(value = "文章管理 controller" ,tags = {"文章管理接口"})
public class ArticleController extends BaseController{

    @Autowired
    ArticleService articleService;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    ArticleLikesRecordService articleLikesRecordService;

    @Autowired
    ArticleCommentService articleCommentService;

    @Autowired
    ArticleCommentReplyService articleCommentReplyService;


    /**
     * 发布文章
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增文章")
    public Result publishArticle(@Validated @RequestBody Article article,Principal principal) {

        String username = principal.getName();
        SysUser sysUser = sysUserService.getByUsername(username);
        article.setAuthor(sysUser.getUsername());
        return Result.succ(articleService.publishArticle(article));
    }
    /**
     * 修改文章
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改文章")
    public Result updateArticle(@Validated @RequestBody Article article, Principal principal ) {

        return Result.succ(articleService.updateArticle(article));
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/del/{ids}")
    @ApiOperation(value = "删除文章")
    public Result delArticle(@RequestBody Long[] ids) {

        articleService.removeByIds(Arrays.asList(ids));

        return Result.succ("删除成功");

    }

    /**
     * 查询我的文章
     */
    @GetMapping("/myArticles")
    @ApiOperation(value = "查询我的文章")
    public Result myArticle(Principal principal) {
        String username = principal.getName();
        Page<Article> pageData = articleService.page(getPage(), new QueryWrapper<Article>()
                .like("author", username));

        return Result.succ(pageData);
    }

    /**
     * 点赞文章
     */
    @PostMapping("/likeArticle/{articleId}")
    @ApiOperation(value = "点赞文章")
    public Result likeArticle(@PathVariable String articleId, @AuthenticationPrincipal Principal principal) {

        String username = principal.getName();
        SysUser user = sysUserService.getByUsername(username);

        System.out.print(articleId + ",用户id:"+user.getId()+"----");
        if(articleLikesRecordService.isLike(articleId, user.getId())) {
            return Result.fail("该文章已经被点赞");
        }

        articleService.updateLickArticle(articleId);
        ArticleLikesRecord articleLikesRecord = new ArticleLikesRecord();
        articleLikesRecord.setArticleId(articleId);
        articleLikesRecord.setLikerId(user.getId());
        articleLikesRecord.setIsRead(1);
        articleLikesRecord.setLikeDate(LocalDateTime.now());
        articleLikesRecordService.insertArticleLikesRecord(articleLikesRecord);

        return Result.succ("点赞成功");
    }

    /**
     * 评论文章
     */
    @PostMapping("/addBookComment")
    @ApiOperation(value = "评论文章")
    public Result addBookComment(@Validated @RequestBody ArticleComment articleComment, @AuthenticationPrincipal Principal principal ) {
        SysUser user = sysUserService.getByUsername(principal.getName());
        articleCommentService.addArticleComment(user.getId(),articleComment);
        return Result.succ("评论成功");
    }


    /**
     * 回复评论
     */
    @PostMapping("/replyComment")
    @ApiOperation(value = "回复评论")
    public Result replyComment( @Validated @RequestBody ArticleCommentReply articleCommentReply, @AuthenticationPrincipal Principal principal) {

        SysUser user = sysUserService.getByUsername(principal.getName());
        articleCommentReplyService.replyComment(user.getId(),articleCommentReply);
        return Result.succ("评论成功");

    }










}

